package com.nowcoder.community.entity;/**
 * 功能描述
 *
 * @author 21775
 * @date 2022/09/14  16:08
 */

import lombok.extern.slf4j.Slf4j;

/**
 *@ClassName Page
 *@Description TODO 分页信息
 *@Author 21775
 *@Date 2022/9/14 16:08
 *@Version 1.0
 */
@Slf4j
public class Page {
    private Integer current=1; //当前页数
    private Integer limit=10; //每页行数
    private Integer rows; //总行数
    private String path; //分页查询路径

//    public Page() {
//        log.info("------->自动注入成功");
//    }

    public Integer getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
        if(current>0){
            this.current = current;
        }
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        if(limit>0&&limit<100){
            this.limit = limit;
        }
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        if(rows>0){
            this.rows = rows;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    /**
     * 获取当前页的起始行
     *
     * @return
     */
    public int getOffset() {
        // current * limit - limit
        return (current - 1) * limit;
    }

    /**
     * 获取总页数
     *
     * @return
     */
    public int getTotal() {
        // rows / limit [+1]
        if (rows % limit == 0) {
            return rows / limit;
        } else {
            return rows / limit + 1;
        }
    }

    /**
     * 获取起始页码
     *
     * @return
     */
    public int getFrom() {
        int from = current - 2;
        return Math.max(from, 1);
    }

    /**
     * 获取结束页码
     *
     * @return
     */
    public int getTo() {
        int to = current + 2;
        return Math.min(to, getTotal());
    }

    @Override
    public String toString() {
        return "Page{" +
                "current=" + current +
                ", limit=" + limit +
                ", rows=" + rows +
                ", path='" + path + '\'' +
                '}';
    }
}
